package com.yancy.dao.impl;

import com.yancy.dao.UserDao;
import com.yancy.entity.New;
import com.yancy.entity.User;
import com.yancy.util.DataSourceUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements UserDao {
    public User userLogin(String userName){
        User user = new User(null,null);
        String sql = "select * from user where userName=?";
        try (Connection conn = DataSourceUtils.getConnection();
             PreparedStatement st = conn.prepareStatement(sql)) {
            st.setString(1, userName);
            try (ResultSet rs = st.executeQuery()) {
                while (rs.next()) {
//                    获取数据库中的账户和密码
                    user.setUserName(rs.getString("userName"));
                    user.setPassWord(rs.getString("passWord"));
                }
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return user;
    }

    @Override
    public List<User> getUser() {
        List<User> list = new ArrayList<>();
        String sql = "select * from user";
        try(Connection conn = DataSourceUtils.getConnection();
            PreparedStatement st = conn.prepareStatement(sql)
        ){
            try(ResultSet rs = st.executeQuery()){
                while (rs.next()){
                    User t = new User(rs.getString("id"),rs.getString("userName"),  rs.getString("passWord"), rs.getTimestamp("inserttime"));
                    list.add(t);
                }
            }
        }catch (Exception e){
            System.out.println("addUserException: "+e);
        }
        return list;
    }

    @Override
    public boolean deleteuser(String id) {
        String sql = "delete from user where id = ?";
        try(Connection conn = DataSourceUtils.getConnection();
            PreparedStatement st = conn.prepareStatement(sql)
        ){
            st.setString(1,id);
            if(st.executeUpdate() == 1){
                return true;
            }else{
                return false;
            }
        }catch (Exception e){
            System.out.println("deleteUserException: "+e);
        }
        return false;
    }

    @Override
    public boolean addUser(User user) {
        String sql = "insert into user (id,userName,passWord) values(?,?,?)";
        try(Connection conn = DataSourceUtils.getConnection();
            PreparedStatement st = conn.prepareStatement(sql);
        ){
            st.setString(1,user.getId());
            st.setString(2,user.getUserName());
            st.setString(3,user.getPassWord());
            if(st.executeUpdate() == 1){
                return true;
            }else{
                return false;
            }
        }catch (Exception e){
            System.out.println("addUserException: "+e);
        }
        return false;
    }

    @Override
    public boolean updateUser(User user) {
        String sql = "update user set userName = ?, passWord = ? where id = ?";
        try(Connection conn = DataSourceUtils.getConnection();
            PreparedStatement st = conn.prepareStatement(sql)
        ){
            st.setString(1,user.getUserName());
            st.setString(2,user.getPassWord());
            st.setString(3,user.getId());
            if(st.executeUpdate() == 1){
                return true;
            }else{
                return false;
            }
        }catch (Exception e){
            System.out.println("updateUserException: "+e);
        }
        return false;
    }
}
